#define _CRT_SECURE_NO_WARNINGS 1

#include"stack.h"

void StackInit(Stack* ps)
{
	assert(ps);

	ps->_a = NULL;
	ps->_top = ps->_capacity = 0;
}

void StackDestroy(Stack* ps)
{
	assert(ps);

	free(ps->_a);
	ps->_capacity = ps->_top = 0;
}

void StackPush(Stack* ps, STDataType data)
{
	assert(ps);

	if (ps->_top == ps->_capacity)
	{
		STDataType newcapcity = ps->_capacity == 0 ? 4 : 2 * ps->_capacity;
		Stack* tmp = (Stack*)malloc(ps->_a, newcapcity * sizeof(STDataType));

		if (tmp == NULL)
		{
			perror("malloc fail!");
			exit(1);
		}

		ps->_a = tmp;
		ps->_capacity = newcapcity;
	}

	ps->_a[ps->_top] = data;
	ps->_top++;
}

bool StackEmpty(Stack* ps)
{
	assert(ps);

	return ps->_top == 0;
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	ps->_top--;
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->_a[ps->_top - 1];
}


int StackSize(Stack* ps)
{
	assert(ps);

	return ps->_top - 1;
}

